package cn.stylefeng.guns.modular.file.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.file.entity.File;
import cn.stylefeng.guns.modular.file.entity.FileEntity;
import cn.stylefeng.guns.modular.file.model.params.FileParam;
import cn.stylefeng.guns.modular.file.service.FileService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


/**
 * 文件管理控制器
 *
 * @author rebiekong
 * @Date 2019-10-20 13:17:08
 */
@Controller
@RequestMapping("/file")
public class FileController extends BaseController {

    private String PREFIX = "/file";

    @Autowired
    private FileService fileService;

    /**
     * 跳转到主页面
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/file.html";
    }

    /**
     * 新增页面
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/file_add.html";
    }

    /**
     * 编辑页面
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/file_edit.html";
    }

    /**
     * 文件更新页面
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/fileUpdate")
    public String update() {
        return PREFIX + "/file_update.html";
    }

    /**
     * 新增接口
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FileParam fileParam, @RequestPart("file") MultipartFile file) throws IOException {
        String path = "H:\\p1024\\" + UUID.randomUUID().toString();
        file.transferTo(new java.io.File(path));
        FileEntity fileEntity = new FileEntity();
        fileEntity.setFilePath(path);
        fileEntity.setFileSize(file.getSize());
        fileEntity.setVersion(1);
        this.fileService.add(fileParam, fileEntity);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FileParam fileParam) {
        this.fileService.update(fileParam);
        return ResponseData.success();
    }

    /**
     * 文件更新接口
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/updateFileItem")
    @ResponseBody
    public ResponseData updateFile(@RequestParam("fileId") Long fileId, @RequestPart("file") MultipartFile file) throws IOException {
        String path = "H:\\p1024\\" + UUID.randomUUID().toString();
        file.transferTo(new java.io.File(path));
        FileEntity fileEntity = new FileEntity();
        fileEntity.setFilePath(path);
        fileEntity.setFileSize(file.getSize());
        this.fileService.updateFile(fileId, fileEntity);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FileParam fileParam) {
        this.fileService.delete(fileParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FileParam fileParam) {
        File detail = this.fileService.getById(fileParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author rebiekong
     * @Date 2019-10-20
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FileParam fileParam) {
        return this.fileService.findPageBySpec(fileParam);
    }

}


